/*
 * ItemRefineResource.cpp
 *
 *  Created on: 2013. 9. 9.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "../NUtil/Utils.h"
#include "../NResource/Static.h"
#include "ItemRefineResource.h"

namespace ProjectZ { namespace NResource {

ItemRefineResource::ItemRefineResource()
{
	pItemRefineAD	= new ItemRefineData;
	pItemRefineIOS		= new ItemRefineData;
}

ItemRefineResource::~ItemRefineResource()
{
	delete pItemRefineAD;
	delete pItemRefineIOS;
}

bool ItemRefineResource::LoadResource()
{
	if (false == pItemRefineAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pItemRefineIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

ItemRefineData::Flyweight * ItemRefineResource::GetFly(User * pUser)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetUserInfo().company, pUser->GetUserInfo().sale_code);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pItemRefineAD->GetFly(pUser);

	case NUtil::MarketType::IOS_KAKAO:
		return pItemRefineIOS->GetFly(pUser);

	default:
		return NULL;
	}

	return NULL;
}


ItemRefineData::~ItemRefineData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
ItemRefineData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool ItemRefineData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_item_refine WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		int seq;
		int tmp_company;
		int tmp_salecode;

		ItemRefineData::Flyweight * pFly = new ItemRefineData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> seq >> tmp_company >> tmp_salecode >> pFly->_TYPE >> pFly->_USE
				 >> pFly->_REFINE1 >> pFly->_REFINE2 >> pFly->_REFINE3 >> pFly->_REFINE4
				 >> pFly->_REFINE5 >> pFly->_REFINE6 >> pFly->_REFINE7 >> pFly->_REFINE8
				 >> pFly->_REFINE9 >> pFly->_REFINE10 >> pFly->_REFINE11
				 >> pFly->_REFINE12 >> pFly->_REFINE13 >> pFly->_REFINE14  >> pFly->_REFINE15;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_TYPE] = pFly;
	}

	return true;
}


ItemRefineData::Flyweight * ItemRefineData::GetFly(User * pUser)
{
	bool bEvent = NResource::Static::InstancePtr()->GetEventTime()->IsItemRefineEvent(pUser);

	auto iter = clsFlyTree.rbegin();

	for (; iter != clsFlyTree.rend(); ++iter)
	{
		if (true == iter->second->_USE)
		{
			// 이벤트 기간 여부 확인
			if (true == bEvent)
			{
				return iter->second;
			}
		}
	}

	// 기본 확률 값 리턴
	return clsFlyTree.begin()->second;
}

} /* NResource */ } /* ProjectZ */
